package com.jintian.smart.kernel.autoconfigure.nacos;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 解决com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder的Spring兼容性问题
 * 废弃原因：私库 nacos-spring-context-2.1.0 已修复此问题
 */
@Deprecated
public class NacosConfigBootBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        /**
         * 解决
         * com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder的兼容性问题
         *
         * @see NacosBootConfigurationPropertiesBinder
         */
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
        if (defaultListableBeanFactory.containsBeanDefinition(NacosBootConfigurationPropertiesBinder.BEAN_NAME)) {
            defaultListableBeanFactory.removeBeanDefinition(NacosBootConfigurationPropertiesBinder.BEAN_NAME);
        }
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(NacosBootConfigurationPropertiesBinder.class);
        defaultListableBeanFactory.registerBeanDefinition(NacosBootConfigurationPropertiesBinder.BEAN_NAME, beanDefinitionBuilder.getBeanDefinition());
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    }
}